//js全局配置
// <!--		//配置axios全局基本路径-->
axios.defaults.baseURL="http://localhost:8080"
// <!--		//拓展属性-->
Vue.prototype.$http=axios

//1.axios前置拦截器，作用：所有ajax请求携带token给后端
axios.interceptors.request.use(config=>{
    //携带token
    let uToken =  localStorage.getItem("token");
    if(uToken){
        config.headers['U-TOKEN']=uToken;
    }
    return config;
},error => {
    Promise.reject(error);
});
//3. 使用axios后置拦截器，处理没有登录请求
axios.interceptors.response.use(result=>{
    let data = result.data;
    if(!data.success && data.resultObj==="noLogin"){
        //清除浏览器的历史数据
        localStorage.removeItem("token");
        localStorage.removeItem("logininfo");
        location.href="login.html";
    }
    return result;
},error => {
    Promise.reject(error);
})
//我们自己手撕静态资源拦截器就可以了  -- 一会儿说
let url = location.href;
if(url.indexOf("login.html")  === -1
    && url.indexOf("register.html")  === -1
    && url.indexOf("callback.html")  === -1
    && url.indexOf("binder.html")  === -1
    && url.indexOf("index.html")  === -1){
    //不是登录也不是注册，阻止
    let user = localStorage.getItem('logininfo');
    if(!user){
        localStorage.removeItem("token");
        localStorage.removeItem("logininfo");
        location.href="login.html";
    }
}
/**
 * 解析url获取参数对象
 * @param url
 * @returns {{}}
 */
function parseUrlParams2Obj(url){// http://bugtracker.itsource.cn/callback.html?code=061IduGa1839qC02jlIa1NmHxX1IduGX&state=1
    //获取请求参数
    let paramStr = url.substring(url.indexOf("?")+1);//code=021FdXFa14mYlA016OGa1L3R4X0FdXFO&state=1
    let paramStrs = paramStr.split("&");//[code=021FdXFa14mYlA016OGa1L3R4X0FdXFO,state=1]
    let paramObj = {};
    for(let i=0;i<paramStrs.length;i++){
        let paramName = paramStrs[i].split("=")[0];//code
        let paramValue = paramStrs[i].split("=")[1];//021FdXFa14mYlA016OGa1L3R4X0FdXFO
        paramObj[paramName] = paramValue;// 动态给对象设置属性和值
    }
    return paramObj;
}
